package by.dezhits.makeanote.content;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import by.dezhits.makeanote.R;
import by.dezhits.makeanote.content.contract.NotesContract;
import by.dezhits.makeanote.db.DBHelper;

public class NotesProvider extends ContentProvider {

	private DBHelper dbHelper;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		dbHelper.deleteNotes(selection);
		getContext().getContentResolver().notifyChange(
				NotesContract.NotesColumns.CONTENT_URI, null);
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		dbHelper.addNote(
				values.getAsInteger(getContext().getString(R.string.id)),
				values.getAsString(getContext().getString(R.string.title)),
				values.getAsString(getContext().getString(R.string.text)),
				values.getAsString(getContext().getString(R.string.date)),
				values.getAsString(getContext().getString(R.string.folder)),
				values.getAsString(getContext().getString(R.string.closed)));
		boolean shouldNotify = values.getAsBoolean(getContext().getString(
				R.string.should_notify));
		if (shouldNotify) {
			getContext().getContentResolver().notifyChange(
					NotesContract.NotesColumns.CONTENT_URI, null);
		}
		return null;
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		dbHelper = new DBHelper(getContext(), null);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor cursor = dbHelper.getNotes(selection, selectionArgs, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
